package com.qidian.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.pojo.ProRoute;
import com.qidian.mes.pojo.Proces;
import com.qidian.mes.pro.mapper.RouteMapper;
import com.qidian.mes.pro.service.RouteService;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author chenzheng
 * @Company 起点编程
 * @Time 2023/10/28 10:40
 * @Description
 */
@Service
public class RouteServiceImpl implements RouteService {
    @Autowired
    private RouteMapper routeMapper;
    @Override
    public MESResult queryById(Long routeId) {
        ProRoute proRoute = null;
        if ((proRoute = routeMapper.selectById(routeId)) != null){
            return MESResult.ok(proRoute);
        }
        return MESResult.empty();
    }

    @Override
    public MESResult save(ProRoute proRoute) {
        // 校验路线名是否重复
        if(proRoute.getRouteName() != null && routeMapper.selectByRouteName(proRoute.getRouteName()) != null){
            return MESResult.duplicate("用户名重复");
        }
        // updateIsOk会根据传入的参数判断返回的结果
        return MESResult.updateIsOk(routeMapper.save(proRoute));
    }

    @Override
    public MESResult update(ProRoute route) {
        // 校验是否重复
        Proces route1 = routeMapper.selectByRouteName(route.getRouteName());
        if(route1 != null && !route1.getProcessId().equals(route.getRouteId())){
            return MESResult.duplicate("用户名重复");
        }
        return MESResult.updateIsOk(routeMapper.update(route));
    }

    @Override
    public MESResult delete(Long routeId) {
        return MESResult.updateIsOk(routeMapper.delete(routeId));
    }
    @Override
    public MESResult list(int page, int limit, ProRoute proRoute) {
// 开启分页 在当前线程中执行下一条SQL的时候就会进行分页。
        PageHelper.startPage(page,limit);
        List<ProRoute> list = routeMapper.list(proRoute);
// 组织一个分页对象。（页码，每页大小，最大页码，数据，总条数）
        PageInfo<ProRoute> pageInfo = new PageInfo<>(list);
        return MESResult.ok(pageInfo);
    }
}
